<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header><div>
      <img src="../assets/heima.png" alt="">
      <span>电商后台管理系统</span>

    </div>
    <el-button @click="logout">退出</el-button></el-header>
    <!-- 主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <!-- j将width值指定为一个js表达式，根据是否折叠来确定它的width值 -->
      <el-aside :width="isCollapse?'64px':'200px'">
        <div class="togle-button" @click="togleCollapse">|||</div>
        <!-- 侧边栏菜单区域 -->
        <!-- 只保持一个菜单展开属性 unique-opened,collapse-transition动画效果-->
        <!-- 开启路由模式router，以index值为路径跳转 -->
        <!-- default-active的值是链接地址保持高亮，1点击二级菜单时保存链接地址到sessionstorage，在刚开始加载时sessionstorage中获取这个连接值 -->
        <el-menu
      background-color="#333744"
      text-color="#fff"
      active-text-color="#409EFF"
      :unique-opened="true"
      :collapse="isCollapse"
      :collapse-transition="false"
      :router="true"
      :default-active="activePath">
      <!-- 一级菜单 -->
      <el-submenu :index="keys.id+''" v-for="keys in menulist" :key="keys.id">
        <!-- 一级菜单模板 -->
        <template slot="title">
          <!-- 自定义图标类名动态生成图标 -->
          <i :class="iconsObj[keys.id]"></i>
          <!-- 文本 -->
          <span>{{keys.authName}}</span>
          </template>
          <!-- 二级菜单 -->
         <el-menu-item :index="items.path" v-for="items in keys.children" :key="items.id" @click="saveNavState(items.path)">
           <template slot="title">
             <!-- 图标 -->
          <i class="el-icon-menu"></i>
          <!-- 文本 -->
          <span>{{items.authName}}</span>
           </template>
           </el-menu-item>

      </el-submenu>
    </el-menu></el-aside>
      <!-- 主体部分 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  // 页面刚刚加载时就获取左侧菜单,取出activePath值
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  data: function () {
    return {
    // 左侧菜单
      menulist: [],
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  // 退出功能：1清除token，2返回login页面
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList () {
      // eslint-disable-next-line no-unused-vars
      const { data: res } = await this.$http.get('menus')
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.erro(res.meta.msg)
      this.menulist = res.data
    },
    // 点击按钮折叠、展开菜单
    togleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接激活状态
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>
<style lang="less" scoped>
.el-header{
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
 background-color: #373d41;
 color: #fff;
 font-size: 20px;
 div{
   display: flex;
   align-items: center;
   margin-left: 15px;
   span{
     margin-left: 15px;
   }
 }
}
.el-aside{
  background-color: #333744;
  .el-menu{
    // 取消边框线，解决右边栏隐约对不齐的效果
    border-right: none;
  }
}
  .togle-button{
    background-color: #4a5064;
    height: 24px;
    font-style: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    // 字体空隙
    letter-spacing: 0.2em;

  }

.el-main{
  background-color: #EAEDF1;
}
.home-container{
  height: 100%;
}
.iconfont{
  margin-right: 10px;
}
</style>
